﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

namespace Web
{
    public partial class Card : System.Web.UI.Page
    {


        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        private void day(string d, int year)
        {
            switch (d)
            {
                case "1":
                case "3":
                case "5":
                case "7":
                case "8":
                case "10":
                case "12":
                    daynum("31");
                    break;
                case "2":
                    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
                        daynum("29");
                    else
                        daynum("28");
                    break;
                default:
                    daynum("30");
                    break;
            }
        }

        private void daynum(string num)
        {
            int i;
            ArrayList day = new ArrayList();
            for (i = 1; i <= Convert.ToInt16(num); i++)
                day.Add(i);
            this.DropDownList3.DataSource = day;
            this.DropDownList3.DataBind();
        }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }


        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {              
                    int i;
                    ArrayList month = new ArrayList();
                    for (i = 1; i <= 12; i++)
                        month.Add(i);
                    this.DropDownList2.DataSource = month;
                    this.DropDownList2.DataBind();    //以上初始化月    
                    int j;
                    ArrayList year = new ArrayList();
                    for (j = 1900; j <= 2050; j++)
                        year.Add(j);
                    this.DropDownList1.DataSource = year;
                    this.DropDownList1.DataBind();    //以上初始化年    
                    this.DropDownList1.AutoPostBack = true;
                    this.DropDownList2.AutoPostBack = true;
                    daynum("30");

                    List<Model.CardType> cardTypeList = new List<Model.CardType>();
                    BLL.CardBLL cardBll = (BLL.CardBLL)BLLFAC.DataAccess.CreateCard();
                    if (null != cardBll)
                    {
                        cardTypeList = cardBll.getTypeList();
                        if (0 != cardTypeList.Count) 
                        {
                            DropDownList_Type.DataSource = cardTypeList;
                        }
                    }
               
            }
        }

        protected void AddType_Click(object sender, EventArgs e)
        {
            BLL.CardBLL cardBll = (BLL.CardBLL)BLLFAC.DataAccess.CreateCard();
            string cardInfo = "for test";
            if (null != cardBll)
            {
                Model.CardType cardTypeModel = new Model.CardType();
                cardTypeModel.setCT_cardType(Int32.Parse(TextBox_CardType.Text.Trim()));
                cardTypeModel.setCT_cardValue(Int32.Parse(TextBox_CardValue.Text.Trim()));
                cardTypeModel.setCT_cardInfo(TextBox_CardInfo.Text);
                if (cardBll.addType(cardTypeModel))
                {
                    Label_addCardType.Text = "添加成功";
                    TextBox_CardType.Text = "";
                    TextBox_CardValue.Text = "";
                    TextBox_CardInfo.Text = "";

                }
            }
        }

        protected void GeneReport_Click(object sender, EventArgs e)
        {
            List<Model.Cards> cardList = new List<Model.Cards>();  
            BLL.CardBLL cardBll = (BLL.CardBLL)BLLFAC.DataAccess.CreateCard();


            int num = Int32.Parse(TextBox_Num.Text.Trim());
            //int type = Int32.Parse(TextBox_Type.Text.Trim());
            DateTime dt = DateTime.Now.AddYears(2);

            if (null != cardBll)
            {
                cardList = cardBll.generateCards(num, type, dt);
            }
            if (cardList.Capacity > 0)
            {
                if (cardBll.generateReport(cardList)) {
                    Label_geneCards.Text = "批量生成成功";
                }
            }
            else {
                
            }
        }

        protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
        {
            day(this.DropDownList2.SelectedValue, Convert.ToInt16(this.DropDownList1.SelectedValue));
        }

        protected void DropDownList2_SelectedIndexChanged1(object sender, EventArgs e)
        {
            day(this.DropDownList2.SelectedValue, Convert.ToInt16(this.DropDownList1.SelectedValue));
        }

        

    }
}